package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxFeeType;
import com.adk.backend.service.FeeTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 费用类型管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/fee-type")
public class FeeTypeController {
    
    @Autowired
    private FeeTypeService feeTypeService;
    
    /**
     * 分页查询费用类型列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<LxFeeType>> getFeeTypeList(
            @RequestParam(required = false) String feeName,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer isCost,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("feeName", feeName);
        params.put("type", type);
        params.put("isCost", isCost);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<LxFeeType> result = feeTypeService.getFeeTypeList(params);
        return Result.success(result);
    }
    
    /**
     * 获取所有费用类型（不分页）
     */
    @RequiresPermission("view")
    @GetMapping("/all")
    public Result<List<LxFeeType>> getAllFeeTypes(
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer isCost) {
        
        List<LxFeeType> result = feeTypeService.getAllFeeTypes(type, isCost);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询费用类型
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxFeeType> getFeeTypeDetail(@RequestParam Long id) {
        LxFeeType feeType = feeTypeService.getFeeTypeById(id);
        if (feeType != null) {
            return Result.success(feeType);
        }
        return Result.error("费用类型不存在");
    }
    
    /**
     * 创建费用类型
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxFeeType> createFeeType(@RequestBody LxFeeType feeType) {
        try {
            LxFeeType result = feeTypeService.createFeeType(feeType);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建费用类型失败", e);
            return Result.error("创建费用类型失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新费用类型
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateFeeType(@RequestBody LxFeeType feeType) {
        try {
            boolean success = feeTypeService.updateFeeType(feeType);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新费用类型失败", e);
            return Result.error("更新费用类型失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除费用类型
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteFeeType(@RequestParam Long id) {
        try {
            boolean success = feeTypeService.deleteFeeType(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除费用类型失败", e);
            return Result.error("删除费用类型失败：" + e.getMessage());
        }
    }
}

